#include <iostream>
#include "Alfred.h"

using namespace std;

/*struct Board
{
	char two_d[15][15];

	Board()
	{
		for(size_t i = 0; i < 15; i++)
			for(size_t j = 0; j < 15; j++)
				two_d[i][j] = BLANK;
	}

	bool validate_move(int row, int col)
	{

		//Commented out the print statements
		//We need to validate moves for possible_moves()

		if( row < 1 || row > 15)
		{
			//cerr << "Error: invalid row" << endl;
			return false;
		}
		if( col < 1 || col > 15)
		{
			//cerr << "Error: invalid column" << endl;
			return false;
		}
		if(two_d[row-1][col-1] == BLANK)
			return true;
		else
			//cerr << "Error: space taken " << i<<" "<< j<<endl;

		return false;
	}
};*/

int add_neighbors(Board board, Move move) { 
	int temp_val;
	int  total = 0;
	char piece = move.piece;

	cout << "piece = " << piece << endl;

	//check horizontally
	int shift_val = 0;

	int x_pos = move.row-1;
	int y_pos = move.col-1;

	while(board.two_d[x_pos][y_pos] == piece) {
		if(x_pos > 0) {
			x_pos--;
			shift_val ++;
		} else {
			break;
		}
	}

	x_pos = move.row-1;
	y_pos = move.col-1;

	while(board.two_d[x_pos][y_pos] == piece) {
		if(x_pos < 14) {
			x_pos++;
			shift_val ++;
		} else {
			break;
		}
	}	
	temp_val = 1 <<  shift_val*shift_val;
	cout << "Horizontal Add      = " << temp_val << endl;
	total = total + temp_val;
	
	//check vertically
	shift_val = 0;
	x_pos = move.row-1;
	y_pos = move.col-1;

	while(board.two_d[x_pos][y_pos] == piece) {
		if(y_pos > 0) {
			y_pos--;
			shift_val ++;
		} else {
			break;
		}
	}

	x_pos = move.row-1;
	y_pos = move.col-1;

	while(board.two_d[x_pos][y_pos] == piece) {
		if(y_pos < 14) {
			y_pos++;
			shift_val ++;
		} else {
			break;
		}
	}	
	temp_val = 1 <<  shift_val*shift_val;
	cout << "Vertical Add        = " << temp_val << endl;
	total = total + temp_val;

	//check \ direction
	shift_val = 0;
	x_pos = move.row-1;
	y_pos = move.col-1;

	while(board.two_d[x_pos][y_pos] == piece) {
		if(x_pos > 0 && y_pos > 0 ) {
			x_pos--;
			y_pos--;
			shift_val ++;
		} else {
			break;
		}
	}

	x_pos = move.row-1;
	y_pos = move.col-1;

	while(board.two_d[x_pos][y_pos] == piece) {
		if(x_pos < 14 && y_pos < 14) {
			x_pos++;
			y_pos++;
			shift_val ++;
		} else {
			break;
		}
	}	
	temp_val = 1 <<  shift_val*shift_val;
	cout << "Down Diagonal add   = " << temp_val << endl;
	total = total + temp_val;

	//check / direction
	shift_val = 0;
	x_pos = move.row-1;
	y_pos = move.col-1;

	while(board.two_d[x_pos][y_pos] == piece) {
		if(x_pos < 14 && y_pos > 0 ) {
			x_pos++;
			y_pos--;
			shift_val ++;
		} else {
			break;
		}
	}

	x_pos = move.row-1;
	y_pos = move.col-1;

	while(board.two_d[x_pos][y_pos] == piece) {
		if(x_pos > 0  && y_pos < 14) {
			x_pos--;
			y_pos++;
			shift_val ++;
		} else {
			break;
		}
	}	
	temp_val = 1 << shift_val*shift_val;
	cout << "Up Diagonal add     = " << temp_val << endl;
	total = total + temp_val;

	return total;

}


int evaluate2(Board board, Game game) {
	if( game.moves.size() < 2) {
		return 0;
	}
	Move my_move  = game.moves.at(game.moves.size()-1);	
	Move opp_move = game.moves.at(game.moves.size()-2);

	int my_value  = add_neighbors(board, my_move);
	cout<<" Eval my = " << my_value << endl;
	int opp_value = add_neighbors(board, opp_move);
	cout<<" Eval op = " << opp_value << endl; 

	return (my_value - opp_value);	
}

string display(Board board)
{
	string s;
	stringstream ss;

	ss << ";  1 2 3 4 5 6 7 8 9 a b c d e f" << endl;
	
	for(int i = 0; i < 15; ++i)
	{
		ss << ";" << hex << i+1 << ' ';

		for(int j = 0; j < 15; ++j)
			ss << board.two_d[i][j] << ' ';

		ss << endl;
	}
	
	return ss.str();
}

int main()
{
	Game *game;
	Alfred al = Alfred(game);
	Board board;
	Move m = Move( 2 , 2, BLACK);

	//populate board with some moves
	//board.two_d[13][3] = BLACK;
	board.two_d[3][3] = WHITE;
	board.two_d[1][2] = BLACK;
	board.two_d[3][4] = WHITE;
	/*board.two_d[6][3] = BLACK;
	board.two_d[9][4] = WHITE;
	board.two_d[2][3] = BLACK;
	board.two_d[10][3] = WHITE;
	board.two_d[12][5] = BLACK;
	board.two_d[8][5] = WHITE;
	*/
	board.two_d[1][1] = BLACK;
	cout<<display(board)<<endl;



	int result;
	result = al.add_neighbors(board, m);

	cout<<"Result = "<<result<<endl;

	return 0;
}
